#include "recboxitem.h"
#include "ui_recboxitem.h"

#include<QDebug>
#include<QPropertyAnimation>

RecBoxItem::RecBoxItem(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RecBoxItem)
{
    ui->setupUi(this);

    //先给对应的控件安装事件过滤器才能使用
    ui->musicImageBox->installEventFilter(this);
}

RecBoxItem::~RecBoxItem()
{
    delete ui;
}

void RecBoxItem::setRecText(const QString &text)
{
    ui->recBoxItemText->setText(text);
}

void RecBoxItem::setRecImage(const QString &imagePath)
{

    QString style = "background-image:url("+imagePath+");";
    ui->recMusicImage->setStyleSheet(style);
}

bool RecBoxItem::eventFilter(QObject *watched, QEvent *event)
{
    //对于控件是recMusicImage控件操作
    if(watched == ui->musicImageBox)
    {
        if(event->type() == QEvent::Enter)
        {
            //鼠标进入时设置动画效果
            QPropertyAnimation* animation = new QPropertyAnimation(ui->musicImageBox,"geometry");
            animation->setStartValue(QRect(11,9,ui->musicImageBox->width(),ui->musicImageBox->height()));//起始帧
            animation->setEndValue(QRect(11,0,ui->musicImageBox->width(),ui->musicImageBox->height()));  //结束帧
            animation->setDuration(800);
            //循环次数不设置默认就是1次
            animation->start();

            //动画结束时会发出finished信号，结束时将动画类析构,将它挂入对象树那样析构会造成内存泄漏
            connect(animation,&QPropertyAnimation::finished,this,[=](){
                delete animation;
            });

        }
        else if(event->type() == QEvent::Leave)
        {
            //鼠标离开时设置动画效果
            QPropertyAnimation* animation2 = new QPropertyAnimation(ui->musicImageBox,"geometry");
            animation2->setStartValue(QRect(11,0,ui->musicImageBox->width(),ui->musicImageBox->height()));//起始帧
            animation2->setEndValue(QRect(11,9,ui->musicImageBox->width(),ui->musicImageBox->height()));  //结束帧
            animation2->setDuration(800);
            //循环次数不设置默认就是1次
            animation2->start();
            connect(animation2,&QPropertyAnimation::finished,this,[=](){
                delete animation2;
            });

        }
        return true;
    }

    //如果不是上述事件就交给父类
    return QObject::eventFilter(watched,event);
}
